package example.other;

import org.apache.commons.io.FileUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class ImageSplitter {
    static String pathName = "C:/Users/yangyang/Desktop/申请材料/图片/Screenshot_2023-01-28-00-06-27-626_com.tencent.mm-edit.jpg";
    static String targetPath = "C:/Users/yangyang/Desktop/申请材料/图片/记录4";

    public static void main(String[] args) throws Exception {

        File folder = new File(targetPath);
        // 获取目录下的所有文件
        File[] files = folder.listFiles();
        // 循环删除文件
        for (File file : files) if (file.isFile()) file.delete();

        // 读取图片
        BufferedImage image = ImageIO.read(new File(pathName));
        int width = image.getWidth();
        int height = image.getHeight();
        int chunkHeight = 1700;
        int chunks = (int) Math.ceil(height / (double) chunkHeight);

        // 分割图片
        for (int i = 0; i < chunks; i++) {
            int y = i * chunkHeight;
            int h = (i == chunks - 1) ? height - y : chunkHeight;
            BufferedImage img = image.getSubimage(0, y, width, h);
            File file = FileUtils.getFile(targetPath, "part" + i + ".jpg");
            ImageIO.write(img, "jpg", file);
        }
    }
}
